package com.lcxw.hxzbadmin.controller.giftandcubecontroller;

import com.alibaba.fastjson.JSONObject;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.pojo.GiftType;
import com.lcxw.hxzbadmin.entity.requestbean.GiftTypeModRequestParams;
import com.lcxw.hxzbadmin.service.baseservice.GiftTypeService;
import com.lcxw.hxzbapi.entity.response.Response;

@RestController
@CrossOrigin
@RequestMapping("admin/giftType")
public class GiftTypeController {

    private Logger logger = LoggerFactory.getLogger(GiftTypeController.class);

    @Resource
    GiftTypeService giftTypeService;

    @PostMapping("/mod")
    public Response modGiftType(@RequestBody GiftTypeModRequestParams giftTypeModRequestParams) {
        List<GiftType> giftTypes = giftTypeModRequestParams.getGiftTypes();

        try {
            for (GiftType giftType : giftTypes) {
                if (null == giftTypeService.selectByRange(giftType.getRange())) {
                    giftTypeService.insert(giftType);
                } else {
                    giftTypeService.updateByPrimaryKey(giftType);
                }
            }
        } catch (Exception e) {
            return Response.dbFail(logger, "select gift type by range");
        }

        return Response.common(logger, "mod giftType success.", 0, "success", null);
    }

    @PostMapping("/getAll")
    public Response getAllGiftType(@RequestBody JSONObject requestParams) {
        logger.info("admin {} get all gift Type", requestParams.getString("adminName"));

        List<GiftType> giftTypes;
        try {
            giftTypes = giftTypeService.selectAll();
        } catch (Exception e) {
            return Response.dbFail(logger,"get all gift Type");
        }

        Map<String ,Object> data = new LinkedHashMap<>();
        data.put("giftTypes",giftTypes);

        return Response.common(logger,"get all gift Type success.",0,"success",data);
    }


}
